package com.workmanager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.*;
import com.workmanager.service.LeaveMessageService;
import com.workmanager.service.MessageStudentService;
import com.workmanager.service.MessageTeacherService;
import com.workmanager.service.TalkService;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * Created by haocongshun on 2016/8/29.
 */
@Controller("message")
@RequestMapping("/message")
public class MessageController {
    @Resource(name="messageTeacherService")
    private MessageTeacherService messageTeacherService;
    @Resource(name="messageStudentService")
    private MessageStudentService messageStudentService;
    @Resource(name="talkService")
    private TalkService talkService;
    @Resource(name="leaveMessageService")
    private LeaveMessageService leaveMessageService;

    public LeaveMessageService getLeaveMessageService() {
        return leaveMessageService;
    }

    public void setLeaveMessageService(LeaveMessageService leaveMessageService) {
        this.leaveMessageService = leaveMessageService;
    }

    public TalkService getTalkService() {
        return talkService;
    }

    public void setTalkService(TalkService talkService) {
        this.talkService = talkService;
    }


    public MessageStudentService getMessageStudentService() {
        return messageStudentService;
    }

    public void setMessageStudentService(MessageStudentService messageStudentService) {
        this.messageStudentService = messageStudentService;
    }

    public MessageTeacherService getMessageTeacherService() {
        return messageTeacherService;
    }

    public void setMessageTeacherService(MessageTeacherService messageTeacherService) {
        this.messageTeacherService = messageTeacherService;
    }

    @RequestMapping("/getall")
    public void getall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flagStr = request.getParameter("flag");
        int flag = 0;
        if("getall_tea".equals(flagStr)){
            flag = 1;
        }else if ("getall_stu".equals(flagStr)){
            flag = 2;
        }
        JSONObject json = new JSONObject();
        switch(flag){
            case 1:
                TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
                 Map<String ,Object> mapMessage = messageTeacherService.getall(teacher.getTeacher_id());
                json.put("getall",mapMessage);
                break;
            case 2:
                StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
                Map<String,Object> mapMsg = messageStudentService.getAll(student.getStudent_id());
                mapMsg.put("systemMsg",messageTeacherService.getSystemMessage());
                json.put("getall",mapMsg);
                break;
            default:
                break;
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getFeedback")
    public void getFeedback(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flagStr = request.getParameter("flag");
        int flag = 0;
        if("feedback_tea".equals(flagStr)){
            flag=1;
        }else if("feedback_stu".equals(flagStr)){
            flag =2;
        }
        JSONObject json = new JSONObject();
        switch (flag){
            case 1 :
                TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
                List<FeedbackBean> listFeedbackMessage = messageTeacherService.getFeedbackMessage(teacher.getTeacher_id());
                json.put("feedbackMsg",listFeedbackMessage);
                break;
            case 2 :
                break;
            default:
                break;
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getSystem")
    public void getSystem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        List<SystemMessageBean> listSystemMessage = messageTeacherService.getSystemMessage();
        json.put("systemMsg",listSystemMessage);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getWork")
    public void getWork(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
        List<WorkBean> listWorkMessage = messageTeacherService.getWorkMessage(teacher.getTeacher_id());
        json.put("workMsg",listWorkMessage);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getJobTask")
    public void getJobTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
        List<JobTaskBean> listJobTask = messageStudentService.getJobTask(student.getStudent_id());
        json.put("jobTask",listJobTask);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getEvaluate")
    public void getEvaluate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
        List<EvaluateBean> listEvaluate = messageStudentService.getEvaluate(student.getStudent_id());
        json.put("evaluate",listEvaluate);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/talk")
    public void talk(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String work_id = request.getParameter("id");
        JSONObject json = new JSONObject();
        json.put("talk",talkService.talk(work_id));
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/answer")
    public void answer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flag=request.getParameter("flag");
        String remsg=request.getParameter("remsg");
        String work_id=request.getParameter("work_id");
        JSONObject json = new JSONObject();
        if("stu".equals(flag)){
            StudentBean stu = (StudentBean) request.getSession().getAttribute("学生");
            FeedbackBean feedback = new FeedbackBean();
            feedback.setWork_id(work_id);
            feedback.setFeedback_content(remsg);
            boolean msg = messageStudentService.answer(feedback);
            json.put("msg",msg);
        }
        if("tea".equals(flag)){
            TeacherBean tea = (TeacherBean)request.getSession().getAttribute("教师");
            EvaluateBean evaluate = new EvaluateBean();
            evaluate.setWork_id(work_id);
            evaluate.setEvaluate(remsg);
            boolean msg = messageTeacherService.answer(evaluate);
            json.put("msg",msg);
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/leaveMessage")
    public void leaveMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flag = request.getParameter("flag");
        JSONObject json = new JSONObject();
        if("stu".equals(flag)){
            StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
            String to = request.getParameter("to");
            json.put("leaveMessage",leaveMessageService.getMessage(to,student.getStudent_id()));
        }
        if("tea".equals(flag)){
            TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
            String to = request.getParameter("to");
            json.put("leaveMessage",leaveMessageService.getMessage(teacher.getTeacher_id(),to));
        }
        if("tea_to_stu".equals(flag)){
            TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
            json.put("tea_to_stu",leaveMessageService.getTea_to_Stu(teacher.getTeacher_id()));
        }
        if("stu_to_tea".equals(flag)){
            StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
            json.put("stu_to_tea",leaveMessageService.getStu_to_Tea(student.getStudent_id()));
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/reLeaveMessage")
    public void reLeaveMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String from = request.getParameter("from");
        String to = request.getParameter("to");
        String remsg = request.getParameter("remsg");
        JSONObject json = new JSONObject();
        if("tea".equals(from)){
            TeacherBean teacher = (TeacherBean) request.getSession().getAttribute("教师");
            LeaveMessageBean leaveMessage = new LeaveMessageBean();
            leaveMessage.setLeaveMessage_from("teacher");
            leaveMessage.setTeacher_id(teacher.getTeacher_id());
            leaveMessage.setStudent_id(to);
            leaveMessage.setLeaveMessage_content(remsg);
            json.put("msg",leaveMessageService.reLeaveMessage(leaveMessage));
        }
        if("stu".equals(from)){
            StudentBean student = (StudentBean) request.getSession().getAttribute("学生");
            LeaveMessageBean leaveMessage = new LeaveMessageBean();
            leaveMessage.setLeaveMessage_from("student");
            leaveMessage.setTeacher_id(to);
            leaveMessage.setStudent_id(student.getStudent_id());
            leaveMessage.setLeaveMessage_content(remsg);
            json.put("msg",leaveMessageService.reLeaveMessage(leaveMessage));
        }
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
}
